package com.whx.managea.controller;


import com.whx.managea.entity.City;
import com.whx.managea.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("city")
public class CityController {

    @Autowired
    private CityService cityService;

    //定义用来接受查询所有的请求
    @RequestMapping("findAll")
    public String findAll(Model model){
        //1.调用业务层查询所有的城市
        List<City> citys = cityService.findAll();

        //2.把查询到的所有的城市设置在域中
        //re.setAttr("?","？");
        model.addAttribute("citys", citys);
        return "back/city/index";

    }

    //定义一个请求用来接受添加城市操作
    @RequestMapping("save")
    public String save(City city){
        //1.调用业务逻辑层
        cityService.save(city);
        //2.重新跳转到主页进行展示
        return "redirect:/city/findAll";
    }

    //用来查询所有城市的请求，以json对象返回查询的结果
    @RequestMapping("findAllJSON")
    @ResponseBody
    public List<City> findAllJSON(){
        List<City> cities = cityService.findAll();
        return cities;
    }
}
